package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.POIUtils;
import com.itheima.health.vo.OrderSettingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;


    /**
     * 获取每天的预约限制和已预约人数
     * @param year
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result findAll(Integer year ,Integer month){
        List<OrderSettingVO>  orderList = orderSettingService.finAll(year,month);;
        return  new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS,orderList);
    }

    /**
     * 上传文件
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
public Result  upload(MultipartFile  excelFile){
    try {
        List<String[]> list = POIUtils.readExcel(excelFile);
        if (list!=null && list.size()>0){
            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] strings : list) {
                OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                orderSettingList.add(orderSetting);
            }
            orderSettingService.insert(orderSettingList);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return   new Result(false,MessageConst.IMPORT_ORDERSETTING_FAIL);
    }
    return   new Result(true,MessageConst.IMPORT_ORDERSETTING_SUCCESS);
}


    /**
     * 设置可预约人数
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByDate")
    public   Result  editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumByDate(orderSetting);
        return  new Result(true, MessageConst.ACTION_SUCCESS);
    }


}
